package com.snj.twelve;

import java.time.LocalDate;
import java.time.DayOfWeek;

public class WorkDayCalculator {

    public static LocalDate getNearestWorkDay(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();

        // 如果传入的日期是周一
        if (dayOfWeek == DayOfWeek.MONDAY) {
            return date.minusDays(3); // 返回上一个周五
        }

        // 如果传入的日期是周二到周五
        if (dayOfWeek == DayOfWeek.TUESDAY ||
                dayOfWeek == DayOfWeek.WEDNESDAY ||
                dayOfWeek == DayOfWeek.THURSDAY ||
                dayOfWeek == DayOfWeek.FRIDAY) {
            return date.minusDays(1); // 返回前一天
        }

        // 如果传入的日期是周六
        if (dayOfWeek == DayOfWeek.SATURDAY) {
            return date.minusDays(1); // 返回周五
        }

        // 如果传入的日期是周日
        if (dayOfWeek == DayOfWeek.SUNDAY) {
            return date.minusDays(2); // 返回周五
        }

        // 默认返回传入的日期（理论上不会走到这里）
        return date;
    }

    public static void main(String[] args) {
        LocalDate inputDate = LocalDate.of(2024, 12, 2); // 2024年12月5日是周五
        LocalDate nearestWorkDay = getNearestWorkDay(inputDate);
        System.out.println("传入日期: " + inputDate);
        System.out.println("最近的工作日: " + nearestWorkDay);
    }
}